#include "ReturnFoodState.h"
#include "FoodSearchState.h"
#include "SBSeek.h"
#include "SBSeparation.h"
#include "SBSlowOnApproach.h"
#include "SBFlee.h"

#include "AntWorld.h"
#include "AntHill.h"
#include "ControllablePredator.h"

#include "AIAgent.h"

#include <assert.h>

ReturnFoodState::ReturnFoodState(AIAgent* agent) : AIState(agent)
{
	m_targetDestination = Vec3f(0,2,0);
	m_targetRadiusSqr = 5.0f;

	AntWorld* antWorld = TheAntWorld::Instance();
	AntHill* antHill = antWorld->GetAntHill();
	assert(antHill);
	Predator* pred = antWorld->GetRandPredator();
	ControllablePredator* contPred = antWorld->GetControlledPredator();

	m_targetDestination = antHill->GetPos(); 

	m_behaviourMap.insert(BehaviourAndWeight(new SBSeek( agent, antHill), 1.0f));
	m_behaviourMap.insert(BehaviourAndWeight(new SBSeparation(agent, 3), 1.0f));
	m_behaviourMap.insert(BehaviourAndWeight(new SBSlowOnApproach(agent,  antHill, 1.0f), 1.0f));

	m_behaviourMap.insert(BehaviourAndWeight(new SBFlee(agent, pred, 10.0f), 0.75f));
	m_behaviourMap.insert(BehaviourAndWeight(new SBFlee(agent, contPred, 10.0f), 0.75f));
}

Vec3f ReturnFoodState::Update()
{
	Vec3f diff = m_targetDestination - m_pAgent->GetPos();
	if(diff.LengthSqr() <  m_targetRadiusSqr)
	{
		//Change state
		m_pAgent->SetAIState(new FoodSearchState(m_pAgent));

		return Vec3f();
	}
	else
	{
		return SumForces();
	}
}

const char* ReturnFoodState::GetStateName() const
{
	return "ReturnFoodState";
}